import axios from "./request";
// 请求处理函数
async function processRequest(
  method: string,
  path: string,
  data: any = null
): Promise<any> {
  let response;
  try {
    if (method === "get") {
      response = await axios({ method, url: path });
    } else {
      response = await axios({ method, url: path, data: data });
    }
    return response;
  } catch (error) {
    console.error(
      `Error with ${method.toUpperCase()} request to ${path}:`,
      error
    );
    throw error;
  }
}

// 导出 API 请求函数
export const getData = (path: string) => processRequest("get", path); //  不带参数
export const getQuery = (path: string, data: any) =>
  processRequest("get", path, data); //  携带参数
export const postData = (path: string, data: any) =>
  processRequest("post", path, data);
export const putData = (path: string, data: any) =>
  processRequest("put", path, data);
export const deleteData = (path: string, data: any) =>
  processRequest("delete", path, data);
